<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:insert="~{include :: header('课程列表')}" />
    <th:block th:insert="~{include :: bootstrap-table-css}" />
</head>
<body class="hold-transition">
<div class="container-fluid mt-2 mb-2">
    <div class="card card-primary card-outline card-outline-tabs mb-0">
        <div class="card-body">
            <form id="formId" class="form-inline">
                <input class="form-control mr-1" placeholder="课程名称" type="text" name="courseName"/>
                <a class="btn btn-primary btn-rounded mr-1" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                <a class="btn btn-warning btn-rounded" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
            </form>
        </div>
    </div>
    <div class="btn-group" id="toolbar" role="group">
        <a class="btn btn-success"  onclick="openSetmeal()" shiro:hasPermission="course:add" id="btn-course-add">
            <i class="fa fa-plus"></i> 添加
        </a>
    </div>
    <div class="select-table table-striped">
        <table id="bootstrap-table" class="table-striped"></table>
    </div>
</div>
    <th:block th:insert="~{include :: footer}" />
     <th:block th:insert="~{include :: bootstrap-table-js}" />
    <script th:inline="javascript">
        var prefix = "/course";
        var courseId = [[${courseId}]];
        $(function() {
            var options = {
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                url: prefix + "/package/list.json?courseId="+courseId,
                modalName: "课程",
                columns: [
                {
                    field: 'courseId',
                    title: 'ID'
                },
                {
                    field: 'courseName', 
                    title: '课程名称'
                },
                {
                    field: 'price',
                    title: '价格'
                },
                {
                    field: 'expireDate',
                    title: '有效结束时间',
                    formatter: function(value, row, index) {
                         return row.endTime
                    }
                 },
                {
                    field: 'addTime',
                    title: '添加时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="removeSetmeal(\'' + row.courseId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        //打开课程列表
        function openSetmeal(id) {
            var callback = function(index, layero) {
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.submitHandler(index, layero);
            }
            $.modal.open('课程列表',"/course/package/select/list.html?courseId="+courseId, '90%', '95%',callback);
        }

        //删除套餐
        function removeSetmeal(cid) {
            $.modal.confirm("确定删除该课程吗？", function() {
                $.post(prefix+"/package/remove.json",{parentId:courseId,courseId:cid},function(json){
                    if(json.code === 0){
                        $.modal.msgSuccess("删除成功！");
                        setTimeout(function () {
                            RefreshTable();
                        }, 1000);
                    }else {
                        $.modal.msgSuccess(json.msg);
                    }
                });
            });
        }

        //公用刷新表格
        function RefreshTable() {
            $.table.search();
        }
    </script>
</body>
</html>